package cn.tianyigps.obd.factorys;


import cn.tianyigps.obd.beans.Equipment;
import cn.tianyigps.obd.beans.OBD;
import cn.tianyigps.obd.decode.LengthFieldBasedFrameDecoder;
import cn.tianyigps.obd.utils.MessageCoding;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.StringUtil;

import java.util.List;

/**
 * Created with IDEA
 *
 * @description: 设备和型号路由
 * @author: cui.guibin
 * @create: 2019/3/7 11:05
 **/
public class EquipmentFactory {

    /**
     * @param: data
     * @return Equipment
     * @author cui.guibin
     * @date 2019/3/7 11:17
     * 根据 设备起始位和特定标识 判断型号
     */
    public static Equipment equipmentInstance(String data){

        String strData = data.substring(0, 2).toUpperCase();
        if (StringUtil.isNullOrEmpty(strData))
            return null;
        String equipmentId = null;
        if(data.toUpperCase().startsWith("7E0100") || data.toUpperCase().startsWith("7E0102"))
            equipmentId = data.substring(10, 22).replaceFirst("^0*", "");
        Equipment obd = new OBD(equipmentId, "m2");
        return obd;
    }
    /**
     * @param: data
     * @return Equipment
     * @author cui.guibin
     * @date 2019/3/7 11:17
     * 根据 设备初始化 设备号和型号
     */
    public static void initPipeline(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> list){
        ByteBuf bf = byteBuf.copy();
        byte[] bytes = MessageCoding.getBytes(bf);
        String strData = MessageCoding.bytesToHexString(bytes);
        try {
//            new LengthFieldBasedFrameDecoder(65536, 3, 2, 10 + (strData.length() - strData.replace("7D01", "").replace("7D02", "").length()) / 4, 0).decode(ctx, byteBuf, list);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
